package com.real8.plusplayground.client.person;

import com.google.api.gwt.services.plus.shared.model.Person;
import com.google.api.gwt.services.plus.shared.model.PersonOrganizations;
import com.google.api.gwt.services.plus.shared.model.PersonUrls;
import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.SimpleBeanEditorDriver;
import com.google.gwt.editor.client.adapters.EditorSource;
import com.google.gwt.editor.client.adapters.ListEditor;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class PersonViewer extends Composite implements PersonView {
	interface Driver extends SimpleBeanEditorDriver<Person, PersonViewer> {
	}

	private static PersonReadViewImplUiBinder uiBinder = GWT
			.create(PersonReadViewImplUiBinder.class);

	interface PersonReadViewImplUiBinder extends UiBinder<Widget, PersonViewer> {
	}

	private class PersonOrganizationViewerSource extends
			EditorSource<PersonOrganizationViewer> {
		@Override
		public PersonOrganizationViewer create(int index) {
			PersonOrganizationViewer viewer = new PersonOrganizationViewer();
			organizationContainer.insert(viewer, index);
			return viewer;
		}

		@Override
		public void dispose(PersonOrganizationViewer subEditor) {
			subEditor.removeFromParent();
		}

		@Override
		public void setIndex(PersonOrganizationViewer editor, int index) {
			organizationContainer.insert(editor, index);
		}
	}

	private class PersonUrlViewerSource extends EditorSource<PersonUrlViewer> {
		@Override
		public PersonUrlViewer create(int index) {
			PersonUrlViewer viewer = new PersonUrlViewer();
			urlsContainer.insert(viewer, index);
			return viewer;
		}

		@Override
		public void dispose(PersonUrlViewer subEditor) {
			subEditor.removeFromParent();
		}

		@Override
		public void setIndex(PersonUrlViewer editor, int index) {
			urlsContainer.insert(editor, index);
		}
	}

	private final Driver driver = GWT.create(Driver.class);

	private Presenter presenter;

	@UiField
	TextBox idEditor;

	@UiField
	Label urlEditor;

	@UiField
	Label displayNameEditor;

	@UiField
	PersonNameViewer nameEditor;

	@UiField
	Label nicknameEditor;

	@UiField
	PersonImageViewer imageEditor;

	@UiField
	Label birthdayEditor;

	@UiField
	Label genderEditor;

	@UiField
	Label relationshipStatusEditor;

	@UiField
	Label currentLocationEditor;

	@UiField
	Label taglineEditor;

	@UiField
	Label aboutMeEditor;

	@UiField
	FlowPanel urlsContainer;

	ListEditor<PersonUrls, PersonUrlViewer> urlsEditor;

	@UiField
	FlowPanel organizationContainer;

	ListEditor<PersonOrganizations, PersonOrganizationViewer> organizationsEditor;

	public PersonViewer() {
		initWidget(uiBinder.createAndBindUi(this));
		organizationsEditor = ListEditor
				.of(new PersonOrganizationViewerSource());
		urlsEditor = ListEditor.of(new PersonUrlViewerSource());
		driver.initialize(this);
	}

	@Override
	public void setPresenter(Presenter aPresenter) {
		this.presenter = aPresenter;
	}

	@Override
	public SimpleBeanEditorDriver<Person, ?> getEditorDriver() {
		return driver;
	}

	@Override
	public void setId(String personId) {
		idEditor.setValue(personId);
	}

	@UiHandler("idEditor")
	void onIdEditorKeyPress(KeyPressEvent event) {
		// check for return
		int keyCode = event.getNativeEvent().getKeyCode();
		if (keyCode == 13) {
			doIdChange();
		}
	}

	private void doIdChange() {
		if (presenter != null) {
			String id = idEditor.getValue();

			presenter.onIdChange(id);
		}
	}
}
